<?php
    header('Content-Type: text/html; charset=UTF-8');
    include_once '../../config.php';

    $sql = "SELECT a.address_id FROM address a, project p WHERE project_nr = '" . $_POST['p_nr'] . "' AND a.address_id = p.address_id";
    $result = mysql_query($sql);
    $address = mysql_fetch_array($result);
    $address_id = $address['address_id'];

    //Variablen
    $p_nr         = $_POST['p_nr'];
    $p_clientid   = $_POST['p_client_id'];
    if (empty($_POST['p_date_order'])) {
        $p_dateorder = date('Y-m-d');
    } else {
        $p_dateorder = $_POST['p_date_order'];
    }
    $p_ordersum      = $_POST['p_order_sum'];
    $order_nr_client = $_POST['order_nr_client'];
    $p_nr_client     = $_POST['p_nr_client'];
    $p_street        = $_POST['p_street'];
    $p_zip           = $_POST['p_zip'];
    $p_city          = $_POST['p_city'];
    $statuschoose    = $_POST['status_choose'];
    $p_pm            = $_POST['p_pm_list'];
    $p_comment       = $_POST['p_comment'];

    $tempname = $_FILES['file']['tmp_name'];
    $name = $_FILES['file']['name'];
    $error = $_FILES['file']['error'];
    $size = $_FILES['file']['size'];
    $pfad = '../../assets/uploads';
    $rights = "0777";
    if ($_FILES['file']['error'] == UPLOAD_ERR_OK) {
        $sql = "INSERT INTO file (pid,file,alias,size) VALUES('" . $p_nr . "','" . $name . "','" . $_POST['alias'] . "','" . $size . "')";
        mysql_query($sql);
        $handle = is_dir($pfad.'/'.$p_nr);
        if (!$handle) {
            mkdir($pfad.'/'.$p_nr);
        }
        move_uploaded_file($tempname, '../../assets/uploads/' . $p_nr . '/' . $name);
    }

    if ($p_nr_client == '' || $order_nr_client == '') {
        header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid . '&assumed=nr');
    } else {
        if ($p_pm == 'no') {
            header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid . '&assumed=no');
        } else if ($p_pm == 'new') {
            $pm_name      = $_POST['pm_name'];
            $pm_firstname = $_POST['pm_vorname'];
            $pm_phone     = $_POST['pm_phone'];
            $pm_email     = $_POST['pm_email'];
            $pm_mobil     = $_POST['pm_mobil'];

            if ($pm_name == '') {
                header('Location: ../edit.php?project_nr=' . $p_nr .'&client_id=' . $p_clientid . '&assumed=name');
            } else {
                if ($statuschoose == 0) {
                    $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                         . " VALUES ('" . $p_clientid . "','" . $pm_name . "','" . $pm_firstname . "','" . $pm_phone
                         . "','" . $pm_email . "','" . $pm_mobil . "','pm')";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . mysql_insert_id()
                         . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                         . ", get = 0 WHERE project_nr = '" . $p_nr . "'";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                         . "' WHERE address_id = " . $address_id;
                    mysql_query($sql) or die(mysql_error());

                    header('Location: ../index.php');
                } else if ($statuschoose == 1) {
                    $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                         . " VALUES ('" . $p_clientid . "','" . $pm_name . "','" . $pm_firstname . "','" . $pm_phone
                         . "','" . $pm_email . "','" . $pm_mobil . "','pm')";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . mysql_insert_id()
                         . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                         . "WHERE project_nr = '" . $p_nr . "'";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                         . "' WHERE address_id = " . $address_id;
                    mysql_query($sql) or die(mysql_error());

                    header('Location: ../index.php');
                } else if ($statuschoose == 2) {
                    $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                         . " VALUES ('" . $p_clientid . "','" . $pm_name . "','" . $pm_firstname . "','" . $pm_phone
                         . "','" . $pm_email . "','" . $pm_mobil . "','pm')";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . mysql_insert_id()
                         . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                         . ", get = 2 WHERE project_nr = '" . $p_nr . "'";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                         . "' WHERE address_id = " . $address_id;
                    mysql_query($sql) or die(mysql_error());

                    header('Location: ../index.php');
                } else if ($statuschoose == 3) {
                    $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                         . " VALUES ('" . $p_clientid . "','" . $pm_name . "','" . $pm_firstname . "','" . $pm_phone
                         . "','" . $pm_email . "','" . $pm_mobil . "','pm')";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . mysql_insert_id()
                         . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                         . ", get = 3 WHERE project_nr = '" . $p_nr . "'";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                         . "' WHERE address_id = " . $address_id;
                    mysql_query($sql) or die(mysql_error());

                    header('Location: ../index.php');
                } else {
                    $sql = "INSERT INTO project_manager (client_id,name,vorname,phone,email,mobil,km_or_pm)"
                         . " VALUES ('" . $p_clientid . "','" . $pm_name . "','" . $pm_firstname . "','" . $pm_phone
                         . "','" . $pm_email . "','" . $pm_mobil . "','pm')";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . mysql_insert_id()
                         . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                         . ", get = 4 WHERE project_nr = '" . $p_nr . "'";
                    mysql_query($sql) or die(mysql_error());
                    $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                         . "' WHERE address_id = " . $address_id;
                    mysql_query($sql) or die(mysql_error());

                    header('Location: ../index.php');
                }
            }
        } else {
            if ($statuschoose == 0) {
                $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . $p_pm
                     . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                     . ", get = 0 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                     . "' WHERE address_id = " . $address_id;
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            } else if ($statuschoose == 1) {
               $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . $p_pm
                     . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                     . "WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                     . "' WHERE address_id = " . $address_id;
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            } else if ($statuschoose == 2) {
                $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . $p_pm
                     . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                     . ", get = 2 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                     . "' WHERE address_id = " . $address_id;
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            } else if ($statuschoose == 3) {
                $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . $p_pm
                     . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                     . ", get = 3 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                     . "' WHERE address_id = " . $address_id;
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            } else {
                $sql = "UPDATE project SET date_order = '" . $p_dateorder . "', order_sum = '" . $p_ordersum . "', pm_id = '" . $p_pm
                     . "', project_nr_client = '" . $p_nr_client . "', order_nr_client ='" . $order_nr_client . "', comment = '" . $p_comment . "' "
                     . ", get = 4 WHERE project_nr = '" . $p_nr . "'";
                mysql_query($sql) or die(mysql_error());
                $sql = "UPDATE address SET street = '" . $p_street . "', zip = '" . $p_zip . "', city = '" . $p_city
                     . "' WHERE address_id = " . $address_id;
                mysql_query($sql) or die(mysql_error());

                header('Location: ../index.php');
            }
        }
    }
?>
